package com.czk.codec;

import com.czk.domain.protocol.Packet;
import com.czk.util.SerializationUtil;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

/**
 * 对象编码器
 *
 * @Author:ChenZhangKun
 * @Date: 2021/4/22 11:03
 */
public class ObjEncoder extends MessageToByteEncoder<Packet> {

    @Override
    protected void encode(ChannelHandlerContext channelHandlerContext, Packet packet, ByteBuf byteBuf) throws Exception {
        // 序列化
        byte[] bytes = SerializationUtil.serializer(packet);
        //
        byteBuf.writeInt(bytes.length + 1);
        // 拿到指令
        byteBuf.writeByte(packet.getCommand());
        byteBuf.writeBytes(bytes);
    }
}
